package com.shu.component_base.ui.activity;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.shu.component_base.ui.presenter.BasePresenter;
import com.shu.component_base.ui.view.BaseView;
import com.shu.component_base.utils.view.ToastHelper;

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {
    protected T presenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutYS());
        ARouter.getInstance().inject(this);
        this.presenter = createPresenter();
        if (presenter != null) {
            presenter.attachContext( this);
        }
        initViewOrDataOrEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.removeContext();
        }
    }

    /**
     * 创建presenter
     */
    protected abstract T createPresenter();

    /**
     * 初始化视图和数据
     */
    protected void initViewOrDataOrEvent() {

    }

    /**
     * 返回加载根Layout
     *
     * @return
     */
    public abstract int getLayoutYS();

    /**
     * 弹出Toast
     * @param msg
     */
    @Override
    public void showMsg(String msg) {
        ToastHelper.showToast(msg);
    }
}
